require 'httparty'

# class for request processing
#
module PaylerAPI
  class Request
    include HTTParty
    attr_accessor :method, :params, :response

    def initialize method, params
      self.class.base_uri "https://#{config.host}.payler.com/mapi"
      @method, @params = [method, params.merge(key: config.access_key)]
      @params.merge!(password: config.password) if protected_method?
    end

    def process
      self.response = Response.new \
      self.class.post camel_case_url_path, query: params, headers: headers
    end

    def protected_method?
      [ :refund, :charge, :retrieve ].include? method
    end

    private
      def config; PaylerAPI.configuration; end

      def headers
        {'Content-Type' => 'application/x-www-form-urlencoded'}
      end

      def camel_case_url_path
        '/'+method.to_s.split('_').collect(&:capitalize).join.sub("3ds","3DS")
      end

  end

end
